<?php

class Image
{
    public function water($img, $logo,$pct=30)
    {
        $type=$this->check($img);
        header("Content-type:image/$type");
        $dstImg=$this->ac($type, $img);
        //获取目标图的宽高
        $dstW = imagesx($dstImg);
        $dstH = imagesy($dstImg);
        //源图（小图）
        $srcImg = $this->ac($type,$logo);#封装
        $srcW = imagesx($srcImg);
        $srcH = imagesy($srcImg);
        //目标图位置（X,Y）
        $dstX = $dstW - $srcW;
        $dstY = $dstH - $srcH;
//imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
        //最后一个参数为透明度
        imagecopymerge($dstImg, $srcImg, $dstX, $dstY, 0, 0, $srcW, $srcH, $pct);
//        imagejpeg($dstImg);#封装
        $fn = 'image' .$type;
        $fn($dstImg);
        imagedestroy($dstImg);
        imagedestroy($srcImg);
    }
    public function thumb($img,$w,$h){


        $type=$this->check($img);
        header("Content-type:image/$type");

        $dstW = $w;
        $dstH = $h;
        $dstImg = imagecreatetruecolor($dstW,$dstH);
//源图(大图)
        $srcImg = imagecreatefromjpeg($img);
        $srcW = imagesx($srcImg);
        $srcH = imagesy($srcImg);
        imagecopyresized($dstImg,$srcImg,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
//保存图片
//        imagejpeg($dstImg);
        $fn = 'image' .$type;
        $fn($dstImg);
//销毁资源
        imagedestroy($srcImg);
        imagedestroy($dstImg);
    }
    private function check($img){
        $arr=getimagesize($img);
        return $type = strtolower(ltrim(strrchr($arr['mime'], '/'),'/'));
    }
    private function ac($type,$img){
        $type=$this->check($img);
        $a="imagecreatefrom".$type;
        return $dstImg=$a($img);
    }

}
$obj = new Image();
//$obj->thumb('./img/1.jpg',300 , 150);
$obj->water('./img/3.jpg','./img/2.gif',50);